package mi.payment.utils.transaction;

import android.content.Context;
import android.text.TextUtils;

import java.util.HashMap;
import java.util.Map;

import mi.payment.bean.db.TradeData;
import mi.payment.constants.ParseUtils;
import mi.payment.constants.TransactionBitmap;
import mi.payment.utils.QRCodeTypeUtils;
import mi.payment.utils.iso8583.EncodePackage;
import mi.payment.utils.iso8583.FieldUtils;
import mi.payment.utils.iso8583.MsgField;
import mi.payment.utils.secure.TerminalSecure;

import static mi.payment.utils.QRCodeTypeUtils.TYPE_Alipay;
import static mi.payment.utils.QRCodeTypeUtils.TYPE_No;
import static mi.payment.utils.QRCodeTypeUtils.TYPE_UNION;
import static mi.payment.utils.QRCodeTypeUtils.TYPE_WeChat;

/**
 * 扫二维码支付
 *
 * @author tomcat on 2017/2/17.
 */
public class QRCodeConsumptionTask extends TransactionTask<TradeData, Void, TradeData> {

    protected QRCodeConsumptionTask(Context context) {
        super(context, true);
    }

    @Override
    protected void onBegin(TradeData[] params) {
        setDBStorage(true);
        setUpdateSettlementTable(false);
        tradeData = params[0];
        dbBean = tradeData.getPayDetail();
    }

    @Override
    protected TradeData appointResult() {
        return tradeData;
    }

    @Override
    protected Map<String, MsgField> preAssembly() {
        return EncodePackage.preAssemblyBase(TransactionBitmap.BITMAP_QR_CODE_TRADE);
    }

    @Override
    protected byte[] changeAndAssembly(Map<String, MsgField> fieldMap) {
        dbBean.setProcessCode("660000");
        dbBean.setTransType("22");
        dbBean.setNetManageCode("000");
        dbBean.setTerminalDate(System.currentTimeMillis() / 1000);

        Map<String, MsgField> map = FieldUtils.fillEachFieldValue(fieldMap, tradeData);

        // 填写22域
        MsgField fieldF022 = map.get("F022");
        // 用法描述 031:反扫 032:正扫
        fieldF022.setDataStr("032");
        fieldF022.setDataLength(3);

        MsgField fieldF047 = map.get("F047");
        // 扫出来的二维码数据
        String payQRCode = dbBean.getPayQRCode();

        switch (QRCodeTypeUtils.checkQRCodeType(payQRCode)) {
            case TYPE_Alipay:
                dbBean.transPlatform = 1;
                break;
            case TYPE_WeChat:
                dbBean.transPlatform = 2;
                break;
            case TYPE_UNION:
                dbBean.transPlatform = 4;
                break;
            case TYPE_No:
            default:
                payQRCode = "0000";
        }
        // 填写47域
        // 通道类型 微信-TX01  盛付通钱包-SFT01  支付宝-ZFB01
        String channelType = "TX01";
        if (dbBean.transPlatform == 1) {
            channelType = "ZFB01";
        } else if (dbBean.transPlatform == 2) {
            channelType = "TX01";
        } else if (dbBean.transPlatform == 4) {
            channelType = "CUP01";
        }
        StringBuilder value47 = new StringBuilder();
        value47.append("TXNWAY=");
        value47.append(channelType);
        value47.append("|");
        value47.append("WXCODE=");
        value47.append(payQRCode);
        fieldF047.setDataStr(value47.toString());
        fieldF047.setDataLength(value47.length());

        // 填写60域
        MsgField fieldF060 = map.get("F060");
        String value60 = dbBean.getTransType() + dbBean.getBathNo() + dbBean.getNetManageCode();
        fieldF060.setDataStr(value60);
        fieldF060.setDataLength(value60.length());

        // 添加自定义的商户号
        map = ParseUtils.addCustomTransID(map);

        return EncodePackage.assembly(map, "0200", TerminalSecure.PROCESSREQUEST_NONE);
    }

    @Override
    protected void onUnpacked(HashMap<String, MsgField> resp) {
        MsgField f37 = resp.get("F037");
        if (f37 != null) {
            dbBean.thirdTransNum = f37.getDataStr();
        }
        MsgField f39 = resp.get("F039");
        String f39Return = f39.getDataStr();
        if (TextUtils.equals("00", f39Return)|| TextUtils.equals("A2", f39Return)) {
            //订单已支付
            dbBean.QRCodeTransState = 1;
        } else if (TextUtils.equals("A7", f39Return) || TextUtils.equals("AW", f39Return)|| TextUtils.equals("AA", f39Return)) {
            //订单支付中
            dbBean.QRCodeTransState = 2;
        } else {
            dbBean.QRCodeTransState = -1;
        }
    }


}
